import member from '@src/store/member'
import base from '@src/store/base'
import { getMemberMineInfo, checkWxCardState, getMemberGiveConfig, getCategoryAvailable, getSignBtnStatus } from '@src/api/member';
import { getPageConfig } from '@src/api/base'
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import dayjs from 'dayjs';
import router from '@src/router';
import config from '@src/config'
Component({
	behaviors: [storeBindingsBehavior],
	storeBindings: [{
		store: member,
		fields: ['isMember', 'memberName', 'categoryName', 'categoryImage', 'wxName', 'expireTime',
			'wxHeadImg', 'cardOrder'
		],
		actions: ['getBaseInfo', 'syncWxCard'],
	}, {
		store: base,
		fields: ['themeColor', 'myPageBanners', 'myPageBg', 'perms'],
		actions: ['setECDSStatus'],
	}],
	data: {
		login: false,
		wxCardState: null,
		memberGiveConfig: {},
		accountInfo: {},
		serviceTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
		availableData: null,
		firstLoad: false,
		cardImage: null,
		viewHeight: 0,
		businessId: '',
		SignBtnStatus: '',
		userCenter: [],
		SignBtnStatus: '',
		showGuide: false,
		Guide: false,
		cardId: null,
		refresh: true
	},
	methods: {
		onLoad() {
			if (config.tpl === 'mall') {
				this.getTabBar().select("pages/mine/main")
			} else {
				this.getTabBar().select("minePage")
			}
			this.setData({
				businessId: getApp().globalData.extConfig.businessId
			})
			if (this.data.login) {
				this.getPageConfig()
			}
		},
		onShow() {
			const appOps = wx.getEnterOptionsSync()
			//公众号引导场景值
			let guideScenes = [1011, 1017, 1025, 1047, 1124]
			this.setData({
				showGuide: guideScenes.includes(appOps.scene)
			})
			if (this.data.login) {
				if (!this.data.firstLoad) {
					this.setData({
						firstLoad: true
					})
				} else {
					// 刷新
					this.fetchData();
				}
			}
			if (this.data.isMember && this.data.wxCardState &&
				(!this.data.wxCardState.hasGet || !this.data.wxCardState.hasActive)) {
				// 更新同步卡包状态
				this.checkWxCardSync();
			}
		},
		onAnon() {
			// 匿名登录成功
			this.setData({
				login: false
			})
			this.getPageConfig()
		},
		onLogin() {
			this.setData({
				login: true
			})
			this.fetchData();
			this.checkWxCardSync()
			this.getBtnStatus()
			this.getPageConfig()
			this.setECDSStatus()
		},
		onPullDownRefresh() {
			this.setData({refresh: false})
			wx.stopPullDownRefresh();
			if (this.data.login) {
				this.getBaseInfo();
				this.fetchData();
				this.checkWxCardSync()
				this.getPageConfig()
			}
		},

		fetchData() {
			getMemberGiveConfig().then(res => {
				this.setData({
					"memberGiveConfig": res.data,
					"serviceTime": dayjs(res._header_.Date).format("YYYY-MM-DD HH:mm:ss")
				})
			})
			getMemberMineInfo().then(res => {
				this.setData({
					"accountInfo": res.data,
					"serviceTime": dayjs(res._header_.Date).format("YYYY-MM-DD HH:mm:ss")
				})
			})
			getCategoryAvailable().then(res => {
				this.setData({
					availableData: res.data,
					"serviceTime": dayjs(res._header_.Date).format("YYYY-MM-DD HH:mm:ss")
				})
			})

		},
		getPageConfig() {
			getPageConfig({ type: 'userCenter' })
				.then(res => {
					let Guide = !!res.data?.modules.filter(item => item.type === 'guanzhugongzhonghao')?.length
					this.setData({ userCenter: res.data?.modules, Guide, refresh: true })

				})
		},
		checkWxCardSync() {
			checkWxCardState().then(res => {
				if (res.code === 0) {
					this.setData({
						wxCardState: res.data
					})
				}
			})
		},
		handleLogin() {
			this.selectComponent("#login").requireLogin();
		},
		handleOpenWxCard() {
			if (!this.data.wxCardState) {
				return;
			}
			if (this.data.wxCardState.hasGet) {
				console.log(this.data.wxCardState)
				wx.openCard({
					cardList: [{
						cardId: this.data.wxCardState.cardId,
						code: this.data.wxCardState.userCardCode
					}]
				});
			} else {
				this.syncWxCard();
			}
		},
		handleView(e) {
			console.log(e.currentTarget.dataset)
			let name = e.currentTarget.dataset.name;
			let params = e.currentTarget.dataset.params;
			let path = e.currentTarget.dataset.path;
			let json = e.currentTarget.dataset.json;
			if (!this.data.login) {
				this.handleLogin();
				return;
			}
			if (router.requireMemberPages.indexOf(name) !== -1 && !this.data.isMember) {
				if (name != 'member/balanceRecharge') {
					wx.showModal({
						title: "提示",
						content: "请先成为会员，才能访问该页面",
						cancelText: "取消",
						confirmText: "开卡页面",
						success: (sm) => {
							if (sm.confirm) {
								router.push({
									name: "indexChooseCard"
								})
							}
						}
					});
					return;
				}
			}
			getApp().$router.push({
				name,
				params,
				json,
				path
			})
		},
		getBtnStatus() {
			getSignBtnStatus({}).then(res => {
				if (res.code == 0) {
					this.setData({
						SignBtnStatus: res.data
					})
				}
			})
		},
		imageLoad: function (e) { //获取图片真实宽度
			let imgWidth = e.detail.width;
			let imgHeight = e.detail.height;
			//宽高比
			let ratio = imgWidth / imgHeight;
			//计算的高度值
			let viewHeight = 750 / ratio;
			this.setData({
				viewHeight: viewHeight
			})
		},
	}
})
